/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cdp.cdf.component.metadata.obsolete.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.Classification;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.ComponentParameter;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.datatype.BasicType;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.datatype.ComplexType;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.datatype.DictionaryType;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.datatype.ListType;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.datatype.UnsupportType;

import java.io.IOException;

/**
 * 功能描述:
 *
 * @ClassName: ObseleteComponentParameterSerialzier
 * @Author: Fynn Qi
 * @Date: 2020/2/19 11:35
 * @Version: V1.0
 */
public class ObseleteComponentParameterSerialzier extends JsonSerializer<ComponentParameter> {

    @Override
    public void serialize(ComponentParameter parameter, JsonGenerator gen,
        SerializerProvider serializers) throws IOException {
        JsonNode node = new ObjectMapper().createObjectNode();
        JsonUtil.setPropValue(node, "Id", parameter.getId());
        JsonUtil.setPropValue(node, "Code", parameter.getCode());
        JsonUtil.setPropValue(node, "Name", parameter.getName());
        JsonUtil.setPropValue(node, "Description", parameter.getDescription());
        JsonUtil.setPropValue(node, "IsReturn", parameter.isHasReturnValue());
        JsonUtil.setPropValue(node, "IsRequired", parameter.isRequired());
        JsonUtil.setPropValue(node, "Classification", parameter.getClassification().getValue());
        if (parameter.getClassification() == Classification.BASIC) {
            getBasicTypeJsonNode(parameter, node);
        } else if (parameter.getClassification() == Classification.COMPLEX) {
            getComplexTypeJsonNode(parameter, node);
        } else if (parameter.getClassification() == Classification.DICTIONARY) {
            getDictionaryTypeJsonNode(parameter, node);
        } else if (parameter.getClassification() == Classification.LIST) {
            getListTypeJsonNode(parameter, node);
        } else if (parameter.getClassification() == Classification.UNSUPPORT) {
            getUnSupportTypeJsonNode(parameter, node);
        }
        gen.writeTree(node);
    }

    private void getUnSupportTypeJsonNode(ComponentParameter parameter, JsonNode node) {
        JsonNode dataTypeNode = new ObjectMapper().createObjectNode();
        UnsupportType unsupportType = (UnsupportType) parameter.getDataType();
        JsonUtil.setPropValue(dataTypeNode, "ParamType", unsupportType.getParamType());
        JsonUtil.setPropValue(node, "DataType", dataTypeNode);
    }

    private void getListTypeJsonNode(ComponentParameter parameter, JsonNode node) {
        JsonNode dataTypeNode = new ObjectMapper().createObjectNode();
        ListType listType = (ListType) parameter.getDataType();
        JsonUtil.setPropValue(dataTypeNode, "IsBasic", listType.isBasic());
        JsonUtil.setPropValue(dataTypeNode, "ModelId", listType.getModelId());
        JsonUtil.setPropValue(dataTypeNode, "ModelName", listType.getModelName());

        JsonUtil.setPropValue(node, "DataType", dataTypeNode);
    }

    private void getDictionaryTypeJsonNode(ComponentParameter parameter, JsonNode node) {
        JsonNode dataTypeNode = new ObjectMapper().createObjectNode();
        DictionaryType dictionaryType = (DictionaryType) parameter.getDataType();
        JsonUtil.setPropValue(dataTypeNode, "IsTkeyBasic", dictionaryType.isBasicKey());
        JsonUtil.setPropValue(dataTypeNode, "TKeyModelId", dictionaryType.getKeyModelId());
        JsonUtil.setPropValue(dataTypeNode, "TKey", dictionaryType.getKey());

        JsonUtil.setPropValue(dataTypeNode, "IsTValueBasic", dictionaryType.isBasicValue());
        JsonUtil.setPropValue(dataTypeNode, "TValueModelId", dictionaryType.getValueModelId());
        JsonUtil.setPropValue(dataTypeNode, "TValue", dictionaryType.getValue());

        JsonUtil.setPropValue(node, "DataType", dataTypeNode);
    }

    private void getComplexTypeJsonNode(ComponentParameter parameter, JsonNode node) {
        JsonNode dataTypeNode = new ObjectMapper().createObjectNode();
        ComplexType complexType = (ComplexType) parameter.getDataType();
        JsonUtil.setPropValue(dataTypeNode, "ModelId", complexType.getModelId());
        JsonUtil.setPropValue(dataTypeNode, "ModelName", complexType.getModelName());

        JsonUtil.setPropValue(node, "DataType", dataTypeNode);
    }

    private void getBasicTypeJsonNode(ComponentParameter parameter, JsonNode node) {
        JsonNode dataTypeNode = new ObjectMapper().createObjectNode();
        BasicType basicType = (BasicType) parameter.getDataType();
        JsonUtil.setPropValue(dataTypeNode, "ParamType", basicType.getParamType());
        JsonUtil.setPropValue(node, "DataType", dataTypeNode);
    }
}
